import math

import torch
from d2l.torch import MaskedSoftmaxCELoss, grad_clipping
from torch import nn
from d2l import torch as d2l

from d2l_learn.utils import load_data_nmt


# 进行序列截断或填充
def truncated_pad(line, num_steps, padding_idx):
    if len(line) > num_steps:
        return line[:num_steps]
    return line + [padding_idx] * (num_steps - len(line))

# Encoder-Decoder 抽象类
class Encoder(nn.Module):
    def __init__(self, **kargs):
        super(Encoder, self).__init__(**kargs)

    def forward(self, X, *args):
        raise NotImplementedError

class Decoder(nn.Module):
    def __init__(self, **kargs):
        super(Decoder, self).__init__(**kargs)

    def init_state(self, enc_outputs, *args):
        raise NotImplementedError

    def forward(self, X, state):
        raise NotImplementedError

class EncoderDecoder(nn.Module):
    def __init__(self, encoder, decoder):
        super(EncoderDecoder, self).__init__()
        self.encoder = encoder
        self.decoder = decoder
    def forward(self, enc_X, dec_X, valid_lens):
        enc_outputs = self.encoder(enc_X, valid_lens)
        state = self.decoder.init_state(enc_outputs, valid_lens)
        output = self.decoder(dec_X, state)
        return output[0]
    def predict_step(self, *args):
        pass

# 一些辅助函数
# 1. 位置编码
class PositionalEncoding(nn.Module):
    def __init__(self, num_hiddens, dropout, max_len=1000):
        super(PositionalEncoding, self).__init__()
        self.dropout = nn.Dropout(dropout)
        self.P = torch.zeros(1, max_len, num_hiddens)
        X = (torch.arange(max_len, dtype=torch.float32).reshape(-1, 1) /
             torch.pow(10000, torch.arange(0, num_hiddens, 2, dtype=torch.float32) / num_hiddens))
        self.P[:, :, 0::2] = torch.sin(X)
        self.P[:, :, 1::2] = torch.cos(X)

    def forward(self, X):
        X = X + self.P[:, X.shape[1], :].to(X.device)
        return self.dropout(X)
# 2. 多头自注意力
# 掩蔽函数
def masked_softmax(X, valid_lens):
    def _sequence_mask(X, valid_lens, value):
        max_len = X.shape[1]
        masked = torch.arange(max_len, dtype=torch.float32, device=X.device)[None, :] < valid_lens[:, None]
        X[~masked] = value
        return X


    # 输入的形状为(batch_size, num_queries, num_keys)
    if valid_lens is None:
        return torch.nn.Softmax(dim=-1)(X)
    else:
        shape = X.shape
        if valid_lens.dim() == 1:
            valid_lens = torch.repeat_interleave(valid_lens, shape[1], dim=0)
        else:
            valid_lens = valid_lens.reshape(-1)
        X = _sequence_mask(X.reshape(-1, X.shape[-1]), valid_lens, value=-1e6)
        return nn.functional.softmax(X.reshape(shape), dim=-1)


# 注意力评分函数
class DotProductAttention(nn.Module):
    def __init__(self, dropout):
        super(DotProductAttention, self).__init__()
        self.dropout = nn.Dropout(dropout)

    def forward(self, Q, K, V, valid_lens):
        d = Q.shape[-1]
        scores = torch.bmm(Q, K.transpose(1, 2)) / math.sqrt(d)
        self.attention_weights = masked_softmax(scores, valid_lens)
        return torch.bmm(self.dropout(self.attention_weights), V)


class MultiHeadAttention(nn.Module):
    def __init__(self, num_hiddens, num_heads, dropout=0, use_bias=False, **kwargs):
        super(MultiHeadAttention, self).__init__(**kwargs)
        self.W_q = nn.LazyLinear(num_hiddens, bias=use_bias)
        self.W_k = nn.LazyLinear(num_hiddens, bias=use_bias)
        self.W_v = nn.LazyLinear(num_hiddens, bias=use_bias)
        self.W_o = nn.LazyLinear(num_hiddens, bias=use_bias)
        self.attention = DotProductAttention(dropout)
        self.num_heads = num_heads

    def transpose_qkv(self, X, num_heads):
        X = X.reshape(X.shape[0], X.shape[1], num_heads, -1)
        X = X.permute(0, 2, 1, 3)
        return X.reshape(-1, X.shape[2], X.shape[3])

    def transpose_output(self, X, num_heads):
        # X的形状为
        X = X.reshape(-1, num_heads, X.shape[1], X.shape[2])
        X = X.permute(0, 2, 1, 3)
        return X.reshape(X.shape[0], X.shape[1], -1)

    def forward(self, query, key, value, valid_lens):
        q = self.transpose_qkv(self.W_q(query), self.num_heads)
        k = self.transpose_qkv(self.W_k(key), self.num_heads)
        v = self.transpose_qkv(self.W_v(value), self.num_heads)
        # 处理一下 valid_lens，形状为(batch_size,) or (batch_size, no of queries)
        if valid_lens is not None:
            valid_lens = torch.repeat_interleave(valid_lens, repeats=self.num_heads, dim=0)

        # 这里用到了并行计算的方式将 num_heads和 batch_size 进行相乘，而d_q = d_k = d_v = num_hiddens // num_heads
        # 这样transpose 的 q、k、v的形状为(batch_size * num_heads, no of queries or key_value pairs, d_q)
        output = self.attention(q, k, v, valid_lens)
        outputs = self.transpose_output(output, self.num_heads)
        return self.W_o(outputs)


# 3. 残差连接与层规范化
class AddNorm(nn.Module):
    def __init__(self, normalized_shape, dropout=0):
        super(AddNorm, self).__init__()
        self.dropout = nn.Dropout(dropout)
        self.norm = nn.LayerNorm(normalized_shape)

    def forward(self, X, Y):
        return self.norm(X + self.dropout(Y))

# 5. 前馈神经网络 FNN
class FNN(nn.Module):
    def __init__(self, ffn_outputs, ffn_hiddens, use_bias=False, **kwargs):
        super(FNN, self).__init__(**kwargs)
        self.layer1 = nn.LazyLinear(ffn_hiddens, bias=use_bias)
        self.layer2 = nn.LazyLinear(ffn_outputs, bias=use_bias)
        self.relu = nn.ReLU()

    def forward(self, X):
        X = self.relu(self.layer1(X))
        X = self.layer2(X)
        return X
# 实现 TransformerEncoder
# Encoder Block
class EncoderBlock(nn.Module):
    def __init__(self, num_hiddens, num_heads, fnn_hiddens, dropout, use_bias=False):
        super(EncoderBlock, self).__init__()
        self.attention = MultiHeadAttention(num_hiddens, num_heads, dropout, use_bias)
        self.addNorm1 = AddNorm(num_hiddens,dropout)
        self.fnn = FNN(num_hiddens, fnn_hiddens, use_bias)
        self.addNorm2 = AddNorm(num_hiddens, dropout)

    def forward(self, X, valid_lens):
        Y = self.addNorm1(X, self.attention(X, X, X, valid_lens))
        return self.addNorm2(Y, self.fnn(Y))

class TransformerEncoder(Encoder):
    def __init__(self, vocab_size, embed_size, num_heads, ffn_hiddens, num_layers, dropout, use_bias=False):
        super(TransformerEncoder, self).__init__()
        self.embed_size = embed_size
        self.embedding = nn.Embedding(vocab_size, embed_size)
        self.pos_encoding = PositionalEncoding(embed_size, dropout)
        self.blks = nn.Sequential()
        for i in range(num_layers):
           self.blks.add_module("block" + str(i),
           EncoderBlock(embed_size, num_heads, ffn_hiddens, dropout, use_bias))

    def forward(self, X, valid_lens):
        X = self.pos_encoding(self.embedding(X) * math.sqrt(self.embed_size))
        self.attention_weights = [None] * len(self.blks)
        for i, blk in enumerate(self.blks):
            X = blk(X, valid_lens)
            self.attention_weights[i] = blk.attention.attention.attention_weights
        return X


# 实现 transformer Decoder
# Decoder Block
class DecoderBlock(nn.Module):
    def __init__(self, num_hiddens, num_heads, fnn_hiddens, dropout, i, use_bias=False):
        super(DecoderBlock, self).__init__()
        self.i = i
        self.masked_attention = MultiHeadAttention(num_hiddens, num_heads, dropout, use_bias)
        self.attention = MultiHeadAttention(num_hiddens, num_heads, dropout, use_bias)
        self.addNorm1 = AddNorm(num_hiddens,dropout)
        self.fnn = FNN(num_hiddens, fnn_hiddens, use_bias)
        self.addNorm2 = AddNorm(num_hiddens,dropout)
        self.addNorm3 = AddNorm(num_hiddens,dropout)

    def forward(self, X, state):
        # masked_attention的输入要区分是训练阶段还是预测阶段
        if state[2][self.i] is None: # 训练阶段或者预测阶段的开始都会被初始化成 None
            key_values = X
        else:
            key_values = torch.cat([state[2][self.i], X], dim=1)

        state[2][self.i] = key_values
        if self.training:
            batch_size, num_steps, _ = X.shape
            dec_valid_lens = torch.arange(1, num_steps + 1, dtype = torch.long).to(X.device).repeat(batch_size, 1)
        else:
            dec_valid_lens = None

        enc_outputs, enc_valid_lens = state[0], state[1]
        X = self.addNorm1(X, self.masked_attention(X, key_values, key_values, dec_valid_lens))
        Y = self.addNorm2(X, self.attention(X, enc_outputs, enc_outputs, enc_valid_lens))
        return self.addNorm3(Y, self.fnn(Y)), state

class TransformerDecoder(Decoder):
    def __init__(self, embed_size, tgt_vocab_size, num_heads, ffn_hiddens, num_layers, dropout, use_bias=False):
        super(TransformerDecoder, self).__init__()
        self.embed_size = embed_size
        self.num_layers = num_layers
        self.embedding = nn.Embedding(tgt_vocab_size, embed_size)
        self.pos_encoding = PositionalEncoding(embed_size, dropout)
        self.blks = nn.Sequential()
        for i in range(num_layers):
            self.blks.add_module("block" + str(i),
                                 DecoderBlock(embed_size, num_heads, ffn_hiddens, dropout, i, use_bias))
        self.dense = nn.LazyLinear(tgt_vocab_size)

    def init_state(self, enc_outputs, enc_valid_lens, *args):
        return [enc_outputs, enc_valid_lens, [None] * self.num_layers]

    def forward(self, X, state):
        X = self.pos_encoding(self.embedding(X) * math.sqrt(self.embed_size))
        self._attention_weights = [[None] * self.num_layers for _ in range(2)]
        for i, blk in enumerate(self.blks):
            X, state = blk(X, state)
            self._attention_weights[0][i] = blk.masked_attention.attention.attention_weights
            self._attention_weights[1][i] = blk.attention.attention.attention_weights
        return self.dense(X), state

    @property
    def attention_weights(self):
        return self._attention_weights
# train_seq2seq
def train_seq2seq(net, train_iter, lr, num_epochs, tgt_vocab, device):
    # 初始化net 参数
    def xavier_init_weights(m):
        if isinstance(m, nn.Linear):
            nn.init.xavier_uniform_(m.weight)
        if isinstance(m, nn.GRU):
            for param in m._flat_weights_names:
                if "weight" in param:
                    nn.init.xavier_uniform_(m._parameters[param])
    net.apply(xavier_init_weights)
    net.to(device)
    net.train()
    # 定义 loss 和 optimizer
    loss = MaskedSoftmaxCELoss()
    optimizer = torch.optim.Adam(net.parameters(), lr=lr)
    animator = d2l.Animator(xlabel='epoch', ylabel='loss',xlim=[10, num_epochs])
    # 开始训练
    for epoch in range(num_epochs):
        timer = d2l.Timer()
        metric = d2l.Accumulator(2)
        for data in train_iter:
            X, X_valid_lens, Y, Y_valid_lens = [x.to(device) for x in data]
            # 将 Y 拼接
            bos = torch.tensor([tgt_vocab['<bos>']]).repeat(Y.shape[0]).to(device).reshape(-1, 1)
            dec_input = torch.cat([bos, Y[:, :-1]], dim=1)
            output = net(X, dec_input, X_valid_lens)
            l = loss(output, Y, Y_valid_lens)
            optimizer.zero_grad()
            l.sum().backward()
            grad_clipping(net, 1)
            num_tokens = Y_valid_lens.sum()
            optimizer.step()
            with torch.no_grad():
                metric.add(l.sum(), num_tokens)
        if (epoch + 1) % 10 == 0:
            animator.add(epoch + 1, (metric[0] / metric[1], ))
    print(f'loss {metric[0] / metric[1]:.3f}, {metric[1] / timer.stop():.1f} '
                  f'tokens/sec on {str(device)}')

# predict_seq2seq
def predict_seq2seq(net, src_sentence, src_vocab, tgt_vocab, num_steps, device, save_attention_weights=False):
    src_tokens = src_vocab[src_sentence.lower().split(' ')] + [src_vocab['<eos>']]
    enc_valid_lens = torch.tensor(len(src_tokens), device=device)
    src_tokens = truncated_pad(src_tokens, num_steps, src_vocab['<pad>'])
    enc_X = torch.unsqueeze(torch.tensor(src_tokens, dtype=torch.long, device=device), dim=0)
    dec_input = torch.tensor([tgt_vocab['bos']]).to(device).reshape(1, -1)
    outputs, attention_weight_seq = [], []
    enc_outputs = net.encoder(enc_X, enc_valid_lens)
    dec_state = net.decoder.init_state(enc_outputs, enc_valid_lens)
    for i in range(num_steps):
        dec_output, dec_state = net.decoder(dec_input, dec_state)
        dec_input = dec_output.argmax(2)
        pred = dec_input.squeeze(dim=0).type(torch.int32).item()
        # 保存注意力权重
        if save_attention_weights:
            attention_weight_seq.append(net.decoder.attention_weights)
        if pred == tgt_vocab['<eos>']:
            break
        outputs.append(pred)
    return ' '.join(tgt_vocab.to_tokens(outputs)), attention_weight_seq

def main():
    embed_size, num_layers, dropout, batch_size, num_steps = 32, 2, 0.1, 64, 10
    lr, num_epochs, device = 0.005, 200, torch.device('mps') if torch.backends.mps.is_available() else torch.device('cpu')
    ffn_num_hiddens, num_heads = 64, 4
    train_iter, src_vocab, tgt_vocab = load_data_nmt(batch_size, num_steps)
    encoder = TransformerEncoder(len(src_vocab), embed_size, num_heads, ffn_num_hiddens, num_layers, dropout)
    decoder = TransformerDecoder(embed_size, len(tgt_vocab), num_heads, ffn_num_hiddens, num_layers, dropout)
    net = EncoderDecoder(encoder, decoder)

    # 初始化LazyLinear参数
    dummy_X = torch.randint(0, len(src_vocab), (batch_size, num_steps))
    dummy_valid_lens = torch.ones((batch_size,))
    dummy_dec_X = torch.randint(0, len(tgt_vocab), (batch_size, num_steps))
    with torch.no_grad():
        net(dummy_X, dummy_dec_X, dummy_valid_lens)

    train_seq2seq(net, train_iter, lr, num_epochs, tgt_vocab, device)

    engs = ['go .', "i lost .", 'he\'s calm .', 'i\'m home .']
    fras = ['va !', 'j\'ai perdu .', 'il est calme .', 'je suis chez moi .']
    for eng, fra in zip(engs, fras):
        translation, dec_attention_weight_seq = predict_seq2seq(
            net, eng, src_vocab, tgt_vocab, num_steps, device, True)
    print(f'{eng} => {translation}, ',
          f'bleu {d2l.bleu(translation, fra, k=2):.3f}')

# 展示注意力权重
if __name__ == "__main__":
    main()